package com.compass.doctor.ui.patient.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.compass.doctor.ui.patient.modle.CircleModel;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.CustomNotification;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description：
 * Date:2024/10/29 13:43
 */
public class MyCircleView extends View {

    List<CircleModel> circles = new ArrayList<>();
    List<CircleModel> circlesLine = new ArrayList<>();

    boolean isLine;//是否是画线
    String doctorImId;
    int width;
    int height;
    public Path mPath = new Path();


    public MyCircleView(Context context) {
        super(context);
    }

    public MyCircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        Paint paintLine = new Paint();


        for (CircleModel circle : circles) {
            circle.drawSelf(canvas, paint);
        }

        drawLine(canvas, paintLine, mPath);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        CircleModel circle;
        float x = event.getX();
        float y = event.getY();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                //实例化圆
                circle = new CircleModel(x, y);
                if (isLine) {
                    circlesLine.clear();
                    mPath.moveTo(x, y);
                    circlesLine.add(circle);
                } else {
                    circles.add(circle);
                    sendXY(x, y);
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isLine) {
                    if (circlesLine.size() > 0)
                        sendLine(circlesLine);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (isLine) {
                    circlesLine.add(new CircleModel(x, y));
                    mPath.lineTo(x, y);
                }
                break;
        }
        //记录当前触摸点得当前得坐标

        invalidate();
        return true;
    }

    /**
     * 画线
     *
     * @param canvas
     * @param paint
     * @param path
     */
    public void drawLine(Canvas canvas, Paint paint, Path path) {
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(6);
        paint.setColor(Color.BLUE);
        canvas.drawPath(path, paint);

    }


    public void setDoctorImId(String doctorImId) {
        this.doctorImId = doctorImId;
    }

    public void setIsLIne(boolean isLine) {
        this.isLine = isLine;
    }

    /**
     * 清空标注
     */
    public void clearCircles() {
        circles.clear();
        circlesLine.clear();
        mPath.reset();
        invalidate();
        removeXY();
    }

    /**
     * 当前屏幕的宽高
     *
     * @param width
     * @param height
     */
    public void setWidthAndHeight(int width, int height) {
        this.width = width;
        this.height = height;
        Log.e("width", "==" + width);
        Log.e("height", "==" + height);
    }


    private void sendXY(float x, float y) {
//        EMMessage customMessage = EMMessage.createSendMessage(EMMessage.Type.CUSTOM);
//        //event为需要传递的自定义消息事件，比如礼物消息，可以设置event = "gift"
//        EMCustomMessageBody customBody = new EMCustomMessageBody("add");
//        //params类型为Map<String, String>
//        Map<String, String> params = new HashMap<>();
//        params.put("x", x + "");
//        params.put("y", y + "");
//        params.put("width", width + "");
//        params.put("height", height + "");
//        customBody.setParams(params);
//        customMessage.addBody(customBody);
//
//
//       // 构造自定义通知，指定接收者
//        CustomNotification notification = new CustomNotification();
//        notification.setSessionId(doctorImId);
//        notification.setSessionType(SessionTypeEnum.P2P);
//        Map<String, String> params = new HashMap<>();
//        params.put("x", x + "");
//        params.put("y", y + "");
//        params.put("width", width + "");
//        params.put("height", height + "");
//
//
//        notification.setContent(json.toString());
//        // 若接收者不在线，则当其再次上线时，将收到该自定义系统通知。若设置为 true，则再次上线时，将收不到该通知。
//        notification.setSendToOnlineUserOnly(true);
//        // 设置的推送文案
//        notification.setApnsText("the_content_for_apns");
//       // 自定义推送属性
//        Map<String,Object> pushPayload = new HashMap<>();
//        pushPayload.put("key1", "payload 1");
//        pushPayload.put("key2", 2015);
//        notification.setPushPayload(pushPayload);



    }


    private void sendLine(List<CircleModel> lines) {
//        Log.e("发送线数据==", JSON.toJSONString(lines));
//        EMMessage customMessage = EMMessage.createSendMessage(EMMessage.Type.CUSTOM);
//        //event为需要传递的自定义消息事件，比如礼物消息，可以设置event = "gift"
//        EMCustomMessageBody customBody = new EMCustomMessageBody("addLine");
//        //params类型为Map<String, String>
//        Map<String, String> params = new HashMap<>();
//        params.put("lines", JSON.toJSONString(lines));
//        params.put("width", width + "");
//        params.put("height", height + "");
//        customBody.setParams(params);
//        customMessage.addBody(customBody);
//        // to指另一方环信id（或者群组id，聊天室id）
//        customMessage.setTo(doctorImId);
//        EMClient.getInstance().chatManager().sendMessage(customMessage);
    }

    public void removeXY() {
//        EMMessage customMessage = EMMessage.createSendMessage(EMMessage.Type.CUSTOM);
//        //event为需要传递的自定义消息事件，比如礼物消息，可以设置event = "gift"
//        EMCustomMessageBody customBody = new EMCustomMessageBody("remove");
//        customMessage.addBody(customBody);
//        // to指另一方环信id（或者群组id，聊天室id）
//        customMessage.setTo(doctorImId);
//        EMClient.getInstance().chatManager().sendMessage(customMessage);
    }
}
